package geral.tag;

import java.io.IOException;

import javax.faces.component.UIOutput;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import seguranca.helper.SegurancaManagerHelper;


public class PermissaoTag extends UIOutput {

	private String role;

	private Boolean page;

	private boolean temPermissao = false;

	private static final String LOGIN_PAGE = "/erp-semace/login.faces";

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public boolean isPage() {
		return page;
	}

	public void setPage(boolean page) {
		this.page = page;
	}

	@Override
	public String getRendererType() {
		temPermissao = SegurancaManagerHelper.temPermissao(role, null);
		//se tag de página...
		if (page != null && !temPermissao) {
			HttpServletResponse res = (HttpServletResponse) FacesContext
					.getCurrentInstance().getExternalContext().getResponse();
			try {
				res.sendRedirect(LOGIN_PAGE);
			} catch (IOException e) {
				e.printStackTrace();
			}
			FacesContext.getCurrentInstance().responseComplete();
		}
		return temPermissao ? "" : null;
	}

	@Override
	public boolean isRendered() {
		return getRendererType() != null;
	}
}
